﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SSwingJoint : SRigidFunc<HingeJoint2D>
{
    public GameObject Connect;

    public float MaxTorgue = 500;
    public override void SInit()
    {
        base.SInit();

    }
    public override void SetRigid(Rigidbody2D r)
    {
        base.SetRigid(r);
        var motor = rigidComp.motor;
        motor.maxMotorTorque = MaxTorgue;
       
        rigidComp.motor = motor;
        rigidComp.enableCollision = true;
        Debug.Assert(this.Rigid != Connect.transform.parent.GetComponent<Rigidbody2D>());
        rigidComp.connectedBody = Connect.transform.parent.GetComponent<Rigidbody2D>();
        rigidComp.autoConfigureConnectedAnchor = false;
        var pos = this.transform.position;
        rigidComp.connectedAnchor = rigidComp.connectedBody.transform.InverseTransformPoint(pos);
        rigidComp.anchor = rigidComp.attachedRigidbody.transform.InverseTransformPoint(pos);
        rigidComp.useLimits = true;
        var p = Connect.transform.position - transform.position;
        var angle = Mathf.Atan2(p.y, p.x) / Mathf.PI * 180f;
        Debug.Log(rigidComp.jointAngle);
        rigidComp.limits = rigidComp.limits = new JointAngleLimits2D { min =angle, max = angle };
    }
}
